package edu.tec.mty.core;

import edu.tec.mty.core.connection.*;
import edu.tec.mty.core.modules.*;
import edu.tec.mty.core.modules.models.*;

public class Information {
	
	private Server server;
	private Self self;
	private World world;
	private Game game;
	private Messages messages;
	private Parser parser;
//	private Logger logger;
	
	public Information(String server, String player, Dispatcher dispatcher, String side, String teamName, int number, char agentType) {
//		this.logger = new Logger("see_", teamName);
		this.server = new Server(server);
		this.self = new Self(player, side, number);
		this.world = new World(teamName, number, agentType, this.self);
		this.parser = new Parser(this.world, this.self.getReferenceToPlayerModel());
		this.game = new Game(dispatcher, this.self, this.world);
		this.messages = new Messages(this.game, this.self);
//		this.self.start();
//		this.world.start();
//		this.logger.start();
		this.parser.start();
	}
	
	public Server getReferenceToServer() {
		return this.server;
	}
	
	public Game getReferenceToGame() {
		return this.game;
	}
	
	public Self getReferenceToPlayer() {
		return this.self;
	}
	
	public WorldModelFactory getWorldModelFactory() {
		return this.world.getWorldModelFactory();
	}
	
	public BallModelFactory getBallModelFactory() {
		return this.world.getBallModelFactory();
	}
	
	public Messages getReferenceToMessages() {
		return this.messages;
	}
	
	public void updateFromMove(double x, double y) {
		this.self.initPosition(x, y);
	}
	
	public void shutdown() {
//		this.self.shutdown();
//		this.world.shutdown();
//		this.logger.shutdown();
		this.parser.shutdown();
	}
	
	public void processMessage(String message) {
		String messageType = message.substring(1, message.indexOf(' '));	
		if (messageType.equals("sense_body")) {	
			String temporal = message.substring(message.indexOf(' ') + 1);
			String timeString = temporal.substring(0,temporal.indexOf(' '));
			long time = Long.parseLong(timeString);
			this.game.updateTime(time);
			this.self.updateBody(message, this.game.getGlobalTime());
		}
		if (messageType.equals("hear")) {
			this.messages.updateMessages(message);
		}
		if (messageType.equals("change_player_type")) {
			String messageContent = message.substring(1, message.indexOf(')'));
			String[] parameters = messageContent.split(" ");
			if (parameters.length > 2) {
				int playerNumber = Integer.parseInt(parameters[1]);
				int playerType = Integer.parseInt(parameters[2]);
				if (playerNumber == this.self.getPlayerNumber()) {
					this.self.setPlayerType(playerType);
				}
			}
		}
		if (messageType.equals("see")) {
			//this.world.setWorldInfo(message, this.game.getGlobalTime());
			if (Game.GAME_TIME > 0) {
//				this.logger.write(this, message);
			}
			String temporal = message.substring(message.indexOf(' ') + 1);
			String timeString = temporal.substring(0,temporal.indexOf(' '));
			long time = Long.parseLong(timeString);
			World.setGameTimestamp(time);
			this.parser.updateInfo(message, this.game.getGlobalTime());
		}
		if (messageType.equals("see_global")) {
			//this.world.coachProcessInformation(message, this.game.getTime());
		}
	}
	
}
